﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class GIAOXUService : BaseService
    {
        public static bool Check_Insert_GIAOXU(GIAOXU giaoxu)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOXU>().FirstOrDefault(c => c.TENGIAOXU.ToUpper() == giaoxu.TENGIAOXU.ToUpper()
                            && c.MAGIAOHAT == giaoxu.MAGIAOHAT) == null;
            return result;
        }

        public static bool Check_Update_GIAOXU(GIAOXU giaoxu)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOXU>().FirstOrDefault(c => c.TENGIAOXU.ToUpper() == giaoxu.TENGIAOXU.ToUpper()
                            && c.MAGIAOHAT == giaoxu.MAGIAOHAT && c.MAGIAOXU != giaoxu.MAGIAOXU) == null;
            return result;
        }

        public static bool Check_Delete_GIAOXU(string[] strings)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOHO>().FirstOrDefault(c => strings.Contains(c.MAGIAOXU + "")) == null;
            return result;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan_GiaoHat()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Xứ} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<GIAOHAT>()
                .Join(dataContext.GetTable<GIAOPHAN>(), c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                    (c, v) => new { c.MAGIAOHAT, TENGIAOHAT = c.TENGIAOHAT + " - " + v.TENGIAOPHAN })
                .Join(dataContext.GetTable<GIAOXU>(), c => c.MAGIAOHAT, v => v.MAGIAOHAT,
                    (c, v) => new { v.MAGIAOXU, TENGIAOXU = v.TENGIAOXU + " - " + c.TENGIAOHAT }).OrderBy(c => c.TENGIAOXU)
                , "MAGIAOXU", "TENGIAOXU"));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan_GiaoHat_Data()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Xứ} ---", Value = "" });
            list.AddRange(dataContext.GetTable<GIAOHAT>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false)
                .Join(dataContext.GetTable<GIAOPHAN>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false),
                      c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                      (c, v) => new { c.MAGIAOHAT, TENGIAOHAT = c.TENGIAOHAT + " - " + v.TENGIAOPHAN })
                .Join(dataContext.GetTable<GIAOXU>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false),
                      c => c.MAGIAOHAT, v => v.MAGIAOHAT,
                      (c, v) => new { v.MAGIAOXU, TENGIAOXU = v.TENGIAOXU + " - " + c.TENGIAOHAT, v.ISDEFAULT }).OrderBy(
                          c => c.TENGIAOXU)
                .Select(
                    c =>
                    new SelectListItem() { Text = c.TENGIAOXU, Value = c.MAGIAOXU + "", Selected = c.ISDEFAULT ?? false }));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan_GiaoHat(string id)
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Xứ} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<GIAOHAT>().Where(c => c.MAGIAOHAT + "" == id)
                .Join(dataContext.GetTable<GIAOPHAN>(), c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                    (c, v) => new { c.MAGIAOHAT })
                .Join(dataContext.GetTable<GIAOXU>(), c => c.MAGIAOHAT, v => v.MAGIAOHAT,
                    (c, v) => new { v.MAGIAOXU, v.TENGIAOXU }).OrderBy(c => c.TENGIAOXU)
                , "MAGIAOXU", "TENGIAOXU"));
            return list;
        }

    }
}
